package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {

   public List<Role> findAll() {
        return roleDao.findAll();
    }
 @Autowired
 private RoleDao roleDao;
    @Autowired
    private MenuDao menuDao;
    @Autowired
    private PermissionDao permissionDao;
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {

        Page<Object> pages= PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<Role> permissionList=roleDao.findByCondition(queryPageBean.getQueryString());
        PageResult pageResult = new PageResult(pages.getTotal(), pages.getResult());
        return pageResult;
    }

    @Override
    public Map<String, Object> findAllPage() {
        List<Permission> permissionset=roleDao.findPermissionSet();
        LinkedHashSet<Menu> menuset=roleDao.findMenuLinkedHashSet();
        Map<String,Object> map=new HashMap();
        map.put("tableData",permissionset);
        map.put("MenutableData",menuset);
        return map;
    }

    @Override
    public void add(Map<String, Object> map) {

        String formData1 = map.get("formData").toString();
        Role role = JSON.parseObject(formData1, new TypeReference<Role>() {});

        String menuIdsString=map.get("menuIds").toString();
        JSONArray jsArr = JSONObject.parseArray(menuIdsString);
        List<Integer> menuIds = JSONObject.parseArray(jsArr.toJSONString(), Integer.class);


        String permissionString = map.get("permissionIds").toString();
        JSONArray jsArr2 = JSONObject.parseArray(permissionString);
        List<Integer> permissionIds = JSONObject.parseArray(jsArr2.toJSONString(), Integer.class);

        roleDao.add(role);
        Integer roleId = role.getId();
        Map<String,Integer> menumap=new HashMap<String,Integer>();
        for (Integer menuId:menuIds
                ) {
            menumap.put("menuId",menuId);
            menumap.put("roleId",roleId);
            roleDao.addRoleAndMenuRelations(menumap);
        }

        Map<String,Integer> permissionmap=new HashMap<>();
        for(Integer permissionId:permissionIds){
            permissionmap.put("permissionId",permissionId);
            permissionmap.put("roleId",roleId);
            roleDao.addRoleAndPermissionRelations(permissionmap);
        }

    }

    @Override
    public Role findById(Integer id) {

        return roleDao.findById(id);
    }

    @Override
    public HashMap<String, Object> findmenuAndPermissionIdsByRoleId(Integer id) {
        List<Integer> menuIds=roleDao.findMenuIdsByRoleId(id);
        List<Integer>  permissionIds=roleDao.findPermissionIdsByRoleId(id);
        HashMap<String,Object> map=new HashMap<String, Object>();
        map.put("menuIds",menuIds);
        map.put("permissionIds",permissionIds);
        return map;
    }

    @Override
    public void deleteById(Integer id) {
        menuDao.deleteRoleAndMenuRelationsByRoleId(id);
        menuDao.deleteMenuByRoleId(id);
        permissionDao.deleteRoleAndPermissionRelationsByRoleId(id);
        permissionDao.deletePermissionByRoleId(id);
        roleDao.deleteById(id);
    }

    @Override
    public void edit(Map<String, Object> map) {

        String formData1 = map.get("formData").toString();
        Role role = JSON.parseObject(formData1, new TypeReference<Role>() {});

        String menuIdsString=map.get("menuIds").toString();
        JSONArray jsArr = JSONObject.parseArray(menuIdsString);
        List<Integer> menuIds = JSONObject.parseArray(jsArr.toJSONString(), Integer.class);


        String permissionString = map.get("permissionIds").toString();
        JSONArray jsArr2 = JSONObject.parseArray(permissionString);
        List<Integer> permissionIds = JSONObject.parseArray(jsArr2.toJSONString(), Integer.class);

        roleDao.edit(role);
        Integer roleId = role.getId();
        menuDao.deleteRoleAndMenuRelationsByRoleId(roleId);
        permissionDao.deleteRoleAndPermissionRelationsByRoleId(roleId);

        Map<String,Integer> menumap=new HashMap<String,Integer>();
        for (Integer menuId:menuIds
                ) {
            menumap.put("menuId",menuId);
            menumap.put("roleId",roleId);
            roleDao.addRoleAndMenuRelations(menumap);
        }

        Map<String,Integer> permissionmap=new HashMap<>();
        for(Integer permissionId:permissionIds){
            permissionmap.put("permissionId",permissionId);
            permissionmap.put("roleId",roleId);
            roleDao.addRoleAndPermissionRelations(permissionmap);
        }

    }
}
